// EButton.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Effects

Rectangle {
    id: root

    // ==== 外部接口 ====
    property string text: "Button"
    property string iconCharacter: "\uf013"
    property string iconFontFamily: iconFont.name
    signal clicked

    // ==== 样式 ====
    property real radius: 20
    property color buttonColor: theme.textColor
    property color textColor: "#3FC1E8"
    property color iconColor: theme.focusColor
    property bool shadowEnabled: true
    property color shadowColor: theme.shadowColor
    property real pressedScale: 0.96
    property bool iconRotateOnClick: false

    // ==== 尺寸计算 ====
    property real contentScale: 0.4
    property real iconSize: root.height * contentScale
    readonly property real fontSize: iconSize * 0.9

    // ==== 尺寸控制 ====
    readonly property int horizontalPadding: 20
    implicitHeight: 78
    implicitWidth: layout.implicitWidth + horizontalPadding * 2

    color: "transparent"

    transform: Scale {
        id: scale
        origin.x: root.width / 2
        origin.y: root.height / 2
    }

    // ==== 阴影效果 ====
    MultiEffect {
        source: background
        anchors.fill: background
        shadowEnabled: root.shadowEnabled
        shadowColor: root.shadowColor
        shadowBlur: theme.shadowBlur
        shadowHorizontalOffset: theme.shadowXOffset
        shadowVerticalOffset: theme.shadowYOffset
    }

    // ==== 背景 ====
    Rectangle {
        id: background
        anchors.fill: parent
        radius: root.radius
        color: root.buttonColor
    }

    // ==== 内容布局 ====
    RowLayout {
        id: layout
        anchors.centerIn: parent
        spacing: 8

        // 图标
        Text {
            id: iconLabel
            text: root.iconCharacter
            visible: root.iconCharacter !== ""
            color: root.iconColor
            font.pixelSize: root.iconSize
            font.family: root.iconFontFamily
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            Layout.preferredWidth: iconLabel.implicitWidth
            Layout.preferredHeight: iconSize

            transform: Rotation {
                id: iconRotation
                origin.x: iconLabel.width / 2
                origin.y: iconLabel.height / 2
                angle: 0
            }

            PropertyAnimation {
                id: rotateAnimation
                target: iconRotation
                property: "angle"
                from: 0
                to: 360
                duration: 200
                easing.type: Easing.InOutQuad
            }

            SpringAnimation {
                id: restoreRotation
                target: iconRotation
                property: "angle"
                to: 0
                spring: 3
                damping: 0.3
            }
        }

        // 文字
        Text {
            id: label
            text: root.text
            visible: root.text !== ""
            color: root.textColor
            font.pixelSize: root.fontSize
            font.bold: true
            verticalAlignment: Text.AlignVCenter
            Layout.preferredWidth: label.implicitWidth
        }
    }

    // ==== 交互 ====
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: Qt.PointingHandCursor

        onPressed: {
            scale.xScale = root.pressedScale
            scale.yScale = root.pressedScale
            if (root.iconRotateOnClick) rotateAnimation.start()
        }

        onReleased: {
            scale.xScale = 1.0
            scale.yScale = 1.0
            root.clicked()
            if (root.iconRotateOnClick) restoreRotation.start()
        }

        onCanceled: {
            scale.xScale = 1.0
            scale.yScale = 1.0
            if (root.iconRotateOnClick) restoreRotation.start()
        }
    }
}
